package com.example.wan1.view.system.knowledge.level2;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.utils.ColorTheme;
import com.example.wan1.utils.StatusBarUtil;
import com.example.wan1.utils.ToolsUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import javax.inject.Inject;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.android.support.DaggerAppCompatActivity;

public class KnowledgeLevelActivity extends DaggerAppCompatActivity {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.search)
    ImageView mSearch;
    @BindView(R.id.knowledge_tab)
    TabLayout mKnowledgeTab;
    @BindView(R.id.knowledge_vp)
    ViewPager mKnowledgeVp;

    @BindView(R.id.knowledge_tbar)
    Toolbar mToolbar;
    @Inject
    List<Integer> mIds;
    @Inject
    List<String> mTitles;
    @Inject
    List<Fragment> mFragments;
    @Inject
    SubPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ColorTheme colorTheme = new ColorTheme(this);
        colorTheme.loadTheme();
        setContentView(R.layout.activity_knowledge_level);
        ButterKnife.bind(this);

        initTbar();
        initData();
        initView();
    }

    private void initTbar() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    private void initView() {
        mTitle.setText(getIntent().getStringExtra("title"));

//        mAdapter = new SubPagerAdapter(getSupportFragmentManager(),
//                FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mAdapter.setData(mFragments, mTitles);
        mKnowledgeVp.setAdapter(mAdapter);
        //1、将tablayout与viewpager关联起来
        mKnowledgeTab.setupWithViewPager(mKnowledgeVp);
        //设置阴影高度
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mKnowledgeTab.setElevation(0);
        }
        //2、移除tablayout的所有tab
        mKnowledgeTab.removeAllTabs();
        //3、重新添加tab
        for (int i = 0; i < mTitles.size(); i++) {
            mKnowledgeTab.addTab(mKnowledgeTab.newTab().setText(mTitles.get(i)));
        }

        mKnowledgeVp.setCurrentItem(getIntent().getIntExtra("select_item_pos", 0));

        int color = ToolsUtils.getThemeColor(this);
        if (color != 0) {
            mKnowledgeTab.setSelectedTabIndicatorColor(color);
            mKnowledgeTab.setTabTextColors(ColorStateList.valueOf(color));
            mToolbar.setBackgroundColor(color);
            StatusBarUtil.setColor(this, ToolsUtils.getThemeColor(this), 0);
        }

        if (ToolsUtils.getNightMode(this)) {
            StatusBarUtil.setTransparent(this);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
        } else {
            //导航栏着色
            if (ToolsUtils.isOpenNavigationBar(this)) {
                if (ToolsUtils.getThemeColor(this) != 0)
                    getWindow().setNavigationBarColor(ToolsUtils.getThemeColor(this));
                else
                    getWindow().setNavigationBarColor(ToolsUtils.getAttrValue(this));
            }
        }
    }

    private void initData() {
        //mTitles = new ArrayList<>();
        //mFragments = new ArrayList<>();
        //mIds = new ArrayList<>();

        mTitles = getIntent().getStringArrayListExtra("list");
        mIds = getIntent().getIntegerArrayListExtra("ids");

        for (int i = 0; i < mTitles.size(); i++) {
            PerKnowledgeFragment perSystemFragment = new PerKnowledgeFragment().newInstance(mIds.get(i));
            mFragments.add(perSystemFragment);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
